﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using LiBLaB.Networking.Asynchronous;
using LiBLaB.GUI;

namespace AsyncServer
{
    public partial class ServerMainFrm : Form
    {
        Server Server;
        GUI gui;
        public ServerMainFrm()
        {
            InitializeComponent();
        }

        void serverAcceptConn(int client_index,bool status)
        {
            if(status)
                gui.AddToGUI(this, listBox1, "New Client Accepted, ID: " + client_index.ToString()+".");
            else
                gui.AddToGUI(this, listBox1, "Client ID: " + client_index.ToString()+ ", connection closed.");
        }

        void dataReceived(byte[] buffer,int client_index)
        {
            string message = Encoding.ASCII.GetString(buffer, 0, buffer.Length);
            gui.AddToGUI(this, listBox1, "Client ID: " + client_index.ToString()+ ", Said: " + message);
            byte[] data = Encoding.ASCII.GetBytes("Server Replay:" + message);
            Server.Send(data, client_index);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            listBox1.Items.Add("Server started...");
            Server = new Server(9050, serverAcceptConn, dataReceived);
            gui = new GUI();
        }
    }
}
